import axios from 'axios'
import router from '../router'
import {
    Message
} from 'element-ui';
const request = axios.create({
    baseURL: 'http://192.168.1.102:8084',
    timeout: 5000
})
request.interceptors.request.use(config => {
    const token = localStorage.getItem('token')
    if (token) {
        config.headers['Authorization'] = `Bearer ${token}` // 根据后端要求调整字段名
    }

    //config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    return config
}, error => {
    return Promise.reject(error)
})

request.interceptors.response.use(response => {
    console.log('成功', response.status)
    // if (response.data.message == "用户未登录") {
    //     router.push('/login')
    // } else if (response.data.message == "token已失效，请重新登录") {
    //     router.push('/login')
    //     localStorage.removeItem('token')
    // }
    return response.data
}, error => {
    console.log('失败', error.response.status)
    if (error.response.status == 403) {
        Message.error('您没有权限访问该资源！')
    } else if (error.response.status == 401) {
        Message.error('您未登录或者登录已过期！')
        router.push('/login')
    }
    return Promise.reject(error)
})

export default request